package com.gomtel.smartdevice.ui.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.AppCompatCheckBox;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;

import com.gomtel.smartdevice.R;
import com.gomtel.smartdevice.base.WatchAppBaseActivity;
import com.gomtel.smartdevice.bean.UserContent;
import com.gomtel.smartdevice.moudle.db.entity.SignsOfDataInfo;
import com.gomtel.smartdevice.moudle.event.CommonEvent;
import com.gomtel.smartdevice.ui.dialog.ProgressDialog;
import com.gomtel.smartdevice.ui.dialog.ShowDialog;
import com.gomtel.smartdevice.utils.CommonUtils;
import com.gomtel.smartdevice.utils.LogUtil;
import com.gomtel.smartdevice.utils.SystemUtil;
import com.gomtel.smartdevice.widget.LoginWidgetClass;

import java.util.List;

import butterknife.BindView;
import cn.jiguang.share.android.api.JShareInterface;

/**
 * Created by Administrator on 2018/5/6.
 * 登录
 */

public class LoginActivity extends WatchAppBaseActivity implements View.OnClickListener {

    @BindView(R.id.edit_user)
    EditText edit_user;
    @BindView(R.id.delet_username_view)
    View delet_username_view;
    @BindView(R.id.edit_password)
    EditText edit_password;
    @BindView(R.id.see_password_view)
    AppCompatCheckBox see_password_view;
    @BindView(R.id.button_login)
    TextView button_login;
    @BindView(R.id.forget_the_password)
    TextView forget_the_password;
    @BindView(R.id.qq_login)
    RadioButton qq_login;
    @BindView(R.id.wchat_login)
    RadioButton wchat_login;
    @BindView(R.id.title_about)
    TextView title_about;
    private String contentInfo = "";

    private List<String> platformList;

    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    progressDialog.show();
                    loginWidgetClass.initUserInfo();
                    break;
                case 1:
                    progressDialog.dismiss();
                    LoginSuccecd();
                    break;
                case 2:
                    progressDialog.dismiss();
                    loginWidgetClass.RemovePermissions();
                    break;
            }
            return true;
        }
    });
    private LoginWidgetClass loginWidgetClass;
    private ProgressDialog progressDialog;

    @Override
    protected void registerEvent(CommonEvent commonevent) {
        switch (commonevent.getCode()) {

        }
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_login;
    }

    @Override
    protected void initClass() {
        loginWidgetClass = new LoginWidgetClass(dataManager, handler);
        progressDialog = ShowDialog.newInstance().showProgressStatus(this, "获取登录信息中...");
    }

    @Override
    protected void initData() {
        platformList = JShareInterface.getPlatformList();
        if (platformList == null || platformList.isEmpty()) {
            toastUtil.showToast("请检查配置文件是否正确");
        }
    }

    @Override
    protected void initView() {
        title_about.setText(getString(R.string.register));
    }

    @Override
    protected void initListener() {
        delet_username_view.setOnClickListener(this);
        see_password_view.setOnClickListener(this);
        button_login.setOnClickListener(this);
        forget_the_password.setOnClickListener(this);
        qq_login.setOnClickListener(this);
        wchat_login.setOnClickListener(this);
        title_about.setOnClickListener(this);
        AllchageListener(edit_user);
        AllchageListener(edit_password);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.delet_username_view:
                edit_user.setText("");
                break;
            case R.id.see_password_view:
                int type = InputType.TYPE_CLASS_TEXT;
                if (!see_password_view.isChecked()) {
                    type = InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD;
                }
                edit_password.setInputType(type);
                break;
            case R.id.button_login:
                if (EditVerdict(edit_user.getText().toString().trim(), edit_password.getText().toString().trim()))
                    NetWorkLogin();
                break;
            case R.id.forget_the_password:

                break;
            case R.id.qq_login:
                for (int i = 0; i < platformList.size(); i++) {
                    LogUtil.e(TAG, "platformList.get(i) :" + platformList.get(i));
                }
                if (CommonUtils.isQQClientAvailable(getApplicationContext())) {
                    loginWidgetClass.initPermission("QQ");
                } else {
                    CommonUtils.showToast(getApplicationContext(), "请先安装QQ");
                }
                break;
            case R.id.wchat_login:
                if (CommonUtils.isWeixinAvilible(getApplicationContext())) {
                    loginWidgetClass.initPermission("Wechat");
                } else {
                    CommonUtils.showToast(getApplicationContext(), "请先安装微信");
                }
                break;
            case R.id.title_about:
                break;
        }
    }

    //请求接口
    private void NetWorkLogin() {

    }

    private void AllchageListener(final EditText editText) {
        editText.addTextChangedListener(new TextWatcher() {
            private String beforeText;

            @Override
            public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) {
                beforeText = charSequence.toString();
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
                // 输入后的字符
                String afterText = charSequence.toString();
                boolean isValid = true;
                if (!TextUtils.isEmpty(afterText)) {
                    isValid = SystemUtil.isPasswordLegal(afterText);
                    if (!isValid) {
                        // 用户现在输入的字符数减去之前输入的字符数，等于新增的字符数
                        int differ = afterText.length() - beforeText.length();
                        // 如果用户的输入不符合规范，则显示之前输入的文本
                        editText.setText(beforeText);
                        // 光标移动到文本末尾
                        editText.setSelection(afterText.length() - differ);
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
    }

    private boolean EditVerdict(String userName, String content) {
        boolean status = true;
        if (userName.isEmpty() || content.isEmpty()) {
            status = false;
            toastUtil.showToast("用户名或密码为空");
        }
        if (SystemUtil.isPwdLegal(content)) {
            status = false;
            toastUtil.showToast("密码格式错误");
        }
        return status;
    }

    private void LoginSuccecd() {
        SignsOfDataInfo signsOfDataInfo = dataManager.querySignsOfDataInfo(UserContent.UserName);
        if (signsOfDataInfo != null) {
            startActivity(new Intent(LoginActivity.this, HomeActivity.class));
        } else {
            Intent intent = new Intent(LoginActivity.this, PersonageActivtiy.class);
            intent.putExtra("openActivity", "openActivity");
            startActivity(intent);
        }
        finish();
    }
}
